/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import constants.Constants;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Tuan Minh
 */
public class PaymentModel extends AbstractTableModel {
    ArrayList<entity.Payment> list ;
    public PaymentModel(ArrayList<entity.Payment> list){
        this.list=list;
    }
    public final static String[] columnNames = {
        "Mã phiếu","Mã hóa đơn","Mã khách hàng","Họ tên","Tuổi","Giới tính","CMND","Địa chỉ","Số điện thoại","Quốc tịch","Tình trạng"
    };
    public int getRowCount() {
        return list.size();
    }
    public int getColumnCount() {
        return columnNames.length;
    }
 
    public Object getValueAt(int row, int col) {
        switch(col){
            case 0: return list.get(row).getId();
            case 1: return covertToDate(list.get(row).getDate())+list.get(row).getId();
            case 2: return list.get(row).getCustomer().getId();
            case 3: return list.get(row).getCustomer().getName();
            case 4: return list.get(row).getCustomer().getAge();    
            case 5: 
                if(list.get(row).getCustomer().getGender()==Constants.MALE)
                    return "Nam";
                else
                    return "Nữ";
            case 6: return list.get(row).getCustomer().getId_card();
            case 7: return list.get(row).getCustomer().getAddress();
            case 8: return list.get(row).getCustomer().getPhone();
            case 9: return list.get(row).getCustomer().getCountry();
            case 10: return Constants.BOOK_NAME[list.get(row).getCustomer().getInfo().getStatus()];    
            default: return null;  
            }
    }

    public String getColumnName(int columnIndex) {
        return columnNames[columnIndex];
    }
        public void refesh(){
        fireTableDataChanged();
    }
      public void removeRow(int row) {
        list.remove(row);
        fireTableDataChanged();
    }
    public void insertList(entity.Payment e){
        list.add(e);
        fireTableDataChanged();       
    }
    public void editList(int index,entity.Payment e){
        list.remove(index);
        list.add(index, e);
        fireTableDataChanged();    
    }
     private String covertToDate(String date){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date d = null;
            try {
                d = sdf.parse(date);
            } catch (ParseException ex) {
                return date;
            }
            SimpleDateFormat sd = new SimpleDateFormat("ddMMyyyy");
            return sd.format(d);
        }
}
